var editor = {
	sqlOperators: ["SELECT","FROM","DISTINCT","WHERE","OR","IN","BETWEEN","AND","LIKE","ORDER BY","ASC","DESC","COUNT","GROUP BY","HAVING","CREATE","DROP","TRUNCATE","TABLE","DATABASE","INSERT","UPDATE","DELETE"],

	draw:function(doc)
	{
		if(editor.editor)
		{
			editor.editor.parentNode.removeChild(editor.editor);
			editor.editor = false;
		}
		var iframe = document.createElement('iframe');
		iframe.width = '100%';
		iframe.height = '100%';
		iframe.frameBorder = '0';
		iframe.border = '0';
		iframe.id = 'editor';
		iframe.name = 'editor';
		basic.clearContent();
		$('#divMainContent').append(iframe);
		editor.editor = iframe;
		editor.enableDesignMode(doc);
		editor.editorDocument = $(editor.editor.contentWindow.document);
		editor.editorBody = editor.editor.contentWindow.document.body;
		editor.editorDocument.keyup(function(e){
			if(e.keyCode == 32 || e.keyCode == 13)
			{
				editor.setStyle();
			}
		});		
	},

	enableDesignMode:function(doc)
	{
		try
		{
			editor.editor.contentWindow.document.open();
			editor.editor.contentWindow.document.write(
				'<html>'+
					'<head>'+
						'<link rel="stylesheet" type="text/css" href="css/sql.css">'+
					'</head>'+
					'<body>'+doc+'</body>'+
				'</html>'
			);
			editor.editor.contentWindow.document.close();
		}
		catch(e){}

		if (document.contentEditable)
		{
			editor.editor.contentWindow.document.designMode = "On";
			return;
		}
		else if(document.designMode != null)
		{
			try
			{
				editor.editor.contentWindow.document.designMode = "on";
				return;
			}
			catch(e){}
		}
		setTimeout(function(){editor.enableDesignMode(doc);editor.setStyle();}, 500);
	},

	setStyle:function()
	{
		var content = editor.editor.contentWindow.document.body.innerHTML;
		content = content.toString().replace(/\<font.*?\>/gi,'');
		content = content.toString().replace(/\<\/font\>/gi,'');
		content = content.toString().replace(/\<p\>/gi,'');
		content = content.toString().replace(/\<\/p\>/gi,'<BR>');
		content = content.toString().replace(/\&nbsp\;\<BR\>/gi,'');
		for(var op in editor.sqlOperators)
		{
			var operatorRegexp = new RegExp(editor.sqlOperators[op]+' ','gi')
			content = content.replace(operatorRegexp,'<font color="blue">'+editor.sqlOperators[op]+'</font> ');
		}
		editor.editor.contentWindow.document.body.focus();
		editor.editor.contentWindow.document.body.innerHTML = content;
		editor.setAtEnd(editor.editor.contentWindow.document);
	},
	
	setAtEnd: function(TB)
	{
		if(TB.createTextRange)
		{
			
			var FieldRange = TB.createTextRange();
			FieldRange.moveStart('word', TB.value.length-1);
			FieldRange.select();
		}/*
		else if(TB)
		{var c = TB.createRange();
			if(c.selection){alert(1)}
			c.setSelectionRange(10, 0);
		}*/
	}
};